import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text("listview"),
          ),
          body:
//          NormalListView()
//          HorizontalListView()
              buildListView()),
    );
  }

  ListView buildListView() {
    return ListView.builder(
        itemCount: 100, // 没有指定数量无限加载
        itemBuilder: (context, index) {
          print("$index");
          if (index.isOdd) {
            return Divider();
          }
          return ListTile(
            leading: Icon(Icons.favorite),
            title: Text("1111$index"),
            subtitle: Text("aaaa"),
            trailing: Icon(Icons.arrow_right),
            onTap: showToast,
          );
        });
  }
}

class HorizontalListView extends StatelessWidget {
  const HorizontalListView({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView(
      scrollDirection: Axis.horizontal,
      // 使用generate生成ListView
      children: List.generate(100, (index) {
        return Text(
          "$index",
          style: TextStyle(fontSize: 20),
        );
      }),
    );
  }
}

class NormalListView extends StatelessWidget {
  const NormalListView({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: <Widget>[
        ListTile(
          leading: Icon(Icons.favorite),
          title: Text("1111"),
          subtitle: Text("aaaa"),
          trailing: Icon(Icons.arrow_right),
          onTap: showToast,
        ),
        Divider(),
        ListTile(
          leading: Icon(Icons.favorite),
          title: Text("1111"),
        ),
        ListTile(
          leading: Icon(Icons.favorite),
          title: Text("1111"),
        ),
      ],
    );
  }
}

void showToast() {
  Fluttertoast.showToast(
    msg: "onPressed IconButton",
    toastLength: Toast.LENGTH_SHORT,
  );
}
